package copyengine.resource.file.sound
{
	import copyengine.resource.dto.ResInitDTO;
	import copyengine.resource.dto.ResObjectRequestParamDTO;
	import copyengine.resource.file.CEBasicResFile;
	import copyengine.utils.GeneralUtils;

	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;

	public class CESoundResourceFile extends CEBasicResFile
	{
		private var sound:Sound;
		private var resFilePath:String;

		public function CESoundResourceFile()
		{
			super();
		}

		override protected function doInitialize(_resInitDTO:ResInitDTO):void
		{
			sound = new Sound();
			resFilePath = _resInitDTO.filePath;
		}

		override protected function doStartLoadFile():void
		{
			sound.load(new URLRequest(resFilePath));
			GeneralUtils.addTargetEventListener(sound, Event.COMPLETE, onLoaded);
			GeneralUtils.addTargetEventListener(sound, ProgressEvent.PROGRESS, loadFileOnProgress);
			GeneralUtils.addTargetEventListener(sound, IOErrorEvent.IO_ERROR, loadFileOnError, false, 0, true);
		}

		override public function getObject(_dto:ResObjectRequestParamDTO):Object
		{
			return sound;
		}

		private function onLoaded(e:Event):void
		{
			loadFileComplate();
		}

		private function removeListener():void
		{
			GeneralUtils.removeTargetEventListener(sound, Event.COMPLETE, onLoaded);
			GeneralUtils.removeTargetEventListener(sound, ProgressEvent.PROGRESS, loadFileOnProgress);
			GeneralUtils.removeTargetEventListener(sound, IOErrorEvent.IO_ERROR, loadFileOnError);
		}

	}
}
